
#pragma once

#include "utils.hpp"    
#include <unordered_map>





// 一共4个成员函数: 是否在线 , 加入大厅 , 移除大厅 , 通过ID获取链接
class Online_manger
{
public:
    bool if_online(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        return _online_user.count(uid);
    }

    void insert(uint64_t uid , const websocket_server::connection_ptr &conn_ptr)
    {
        Log_ns::Log(Log_ns::Debug) << "插入用户 : " << uid;
        std::unique_lock<std::mutex> lock(_mtx);
        _online_user.emplace(uid, conn_ptr);
    }

    void erase(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        if(_online_user.count(uid))
        {
            _online_user.erase(uid);
        }
    }

    websocket_server::connection_ptr get_conn_by_id(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mtx);
        if(_online_user.count(uid) == 0)
        {
            return nullptr;
        }
        return _online_user[uid];
    }

private:
    std::unordered_map<int , websocket_server::connection_ptr> _online_user;
    std::mutex _mtx;
};